using System;
using System.Collections.Generic;
using Utilities.Miscellaneous;

namespace Utilities.Collections
{
  public class MultiComparer<T> : IComparer<T> where T : IComparable<T>
  {
    private readonly List<IComparer<T>> _comparers;

    public MultiComparer(IEnumerable<IComparer<T>> comparers)
    {
      ValidationUtils.ArgumentNotNull(comparers, "comparers");

      _comparers = new List<IComparer<T>>(comparers);

      ValidationUtils.ArgumentNotNullOrEmpty<IComparer<T>>(_comparers, "comparers");
    }

    public int Compare(T x, T y)
    {
      for (int i = 0; i < _comparers.Count; i++)
      {
        int result = _comparers[i].Compare(x, y);

        if (result != 0)
          return result;
      }

      return 0;
    }
  }
}